package top.drinyeit.weilong.activity;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.base.BaseActivity;
import top.drinyeit.weilong.model.AuthenticationDetailBean;
import top.drinyeit.weilong.presenter.EditCompanyPresenter;
import top.drinyeit.weilong.util.SPUtil;
import top.drinyeit.weilong.util.Validator;

/**
 * Created by wu on 2018/1/8.
 */

public class PhoneActivity extends BaseActivity {

    private ImageView delete_all;//删除按钮
    public TextView save_btn;//昵称
    public EditText nick_name;
    private AuthenticationDetailBean authenticationDetailBean;

    private EditCompanyPresenter editCompanyPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_nickname);

        initView();
        initIntent();
        initData();
        init();
        setTitle(true, "手机号");
    }

    private void initView() {
        delete_all = findViewById(R.id.delete_all);
        nick_name = findViewById(R.id.nick_name);
        save_btn = findViewById(R.id.save_btn);
    }

    private void initIntent() {
        authenticationDetailBean = (AuthenticationDetailBean) getIntent().getSerializableExtra("a");
        nick_name.setText(authenticationDetailBean.getCompany_phone());
    }

    private void initData() {
        editCompanyPresenter = new EditCompanyPresenter(this);
    }

    @Override
    public void init() {
        nick_name.setInputType(InputType.TYPE_CLASS_NUMBER);
        nick_name.setHint("请输入手机号");
        nick_name.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
        delete_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                nick_name.setText("");
            }
        });
        //保存
        save_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!Validator.isMobile(nick_name.getText().toString())) {
                    showToast("请输入正确的手机号");
                } else {
                    Map<String, Object> map = new HashMap<>();
                    map.put("action", "company_authentication");
                    map.put("user_id", SPUtil.getShareString(Constant.USER_ID));
                    map.put("company_phone", nick_name.getText().toString());
                    editCompanyPresenter.editCompany(map);
                }

            }
        });
    }
}
